package com.example.aop.daapc;

import org.aopalliance.aop.Advice;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.support.NameMatchMethodPointcutAdvisor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.example.aop.bnapc.LoggerMethodInterceptor;
import com.example.aop.common.ForumService;
import com.example.aop.common.ForumServiceImpl;
import com.example.aop.common.UserService;
import com.example.aop.common.UserServiceImpl;

@Configuration
public class AppConfig1 {

	// 要创建代理的目标Bean
	@Bean
	public ForumService forumService() {
		return new ForumServiceImpl();
	}

	// 创建Advice或Advisor(Interceptor)
	@Bean
	public Advice loggerMethodInterceptor() {
		return new LoggerMethodInterceptor();
	}

	// 使用Advice创建Advisor
	@Bean
	public NameMatchMethodPointcutAdvisor nameMatchMethodPointcutAdvisor() {
		NameMatchMethodPointcutAdvisor nameMatchMethodPointcutAdvisor = new NameMatchMethodPointcutAdvisor();
		nameMatchMethodPointcutAdvisor.setMappedNames("remove*");
		nameMatchMethodPointcutAdvisor.setAdvice(loggerMethodInterceptor());
		return nameMatchMethodPointcutAdvisor;
	}

	@Bean
	public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
		return new DefaultAdvisorAutoProxyCreator();
	}
}